<?php

namespace Admin\Model;

use Think\Model;

class SystemModel extends Model
{
    protected $trueTableName = 'user';
    protected $_validate = array(
        array('old_password', 'require', '旧密码不能为空！'), //默认情况下用正则进行验证
        array('old_password', 'contrast_password', '原密码不正确', '1', 'callback'),
        array('password', '5,20', '新密码格式不正确', 0, 'length'), // 自定义函数验证密码格式
        array('password', 'check_password', '确认密码不正确', 0, 'confirm'), // 验证确认密码是否和密码一致
    );

    function contrast_password($password)
    {
        return substr(md5(trim($password)), 6, -6) == $_SESSION["user"]["password"];

    }


    protected $_auto = array(
        array('id', 'getId', 3, 'callback'),
        array('email', 'getEmail', 3, 'callback'),
        array('password', 'getPassword', 3, 'callback'),
        array('token', 'getToken', 3, 'callback'),
    );

    protected function getId()
    {
        return $_SESSION['user']['id'];
    }

    protected function getEmail()
    {
        return $_SESSION['user']['email'];
    }

    protected function getPassword($password)
    {

        //echo $password;
        return substr(md5($password), 6, -6);
    }

    protected function getToken()
    {
        return uniqid();
    }

}

